package com.haitaoit.chengquan.module.center_service.adapter.service_order;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.haitaoit.chengquan.R;
import com.haitaoit.chengquan.base.BackCall;
import com.haitaoit.chengquan.module.center_service.model.ServiceFuwuModel;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

/**
 * Created by Administrator on 2016/10/18.
 */
public class ServiceFuwuAdapter extends BaseAdapter {
    private Context                context;
    private BackCall               backCall;
    private List<ServiceFuwuModel> list;
    private String                 title_type;

    public void setTitle_type(String title_type) {
        this.title_type = title_type;
    }

    public void setBackCall(BackCall backCall) {
        this.backCall = backCall;
    }

    public ServiceFuwuAdapter(Context context) {
        this.context = context;
    }

    public List<ServiceFuwuModel> getList() {
        return list;
    }

    public void setList(List<ServiceFuwuModel> list) {
        this.list = list;
    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.service_fuwu_item, null);
            holder = new ViewHolder();
            ViewUtils.inject(holder, convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.fuwu_itemll:
                    case R.id.item_ll:
                        ServiceFuwuAdapter.this.backCall.deal(R.id.fuwu_itemll, position);
                        break;
                    case R.id.fuwu_check_eva:
                        ServiceFuwuAdapter.this.backCall.deal(R.id.fuwu_check_eva, position);
                        break;
                    case R.id.confirm_complete:
                        ServiceFuwuAdapter.this.backCall.deal(R.id.confirm_complete, position);
                        break;
                    case R.id.fuwu_to_eva:
                        ServiceFuwuAdapter.this.backCall.deal(R.id.fuwu_to_eva, position);
                        break;
                    case R.id.detele_fuwu:
                        ServiceFuwuAdapter.this.backCall.deal(R.id.detele_fuwu, position);
                        break;
                    case R.id.fuwu_submit_order:
                        ServiceFuwuAdapter.this.backCall.deal(R.id.fuwu_submit_order, position);
                        break;
                    case R.id.fuwu_update_order:
                        ServiceFuwuAdapter.this.backCall.deal(R.id.fuwu_update_order, position);
                        break;
                }
            }
        };
        holder.fuwu_itemll.setOnClickListener(listener);
        holder.item_ll.setOnClickListener(listener);
        holder.confirm_complete.setOnClickListener(listener);
        holder.detele_fuwu.setOnClickListener(listener);
        holder.fuwu_check_eva.setOnClickListener(listener);
        holder.fuwu_to_eva.setOnClickListener(listener);
        holder.fuwu_submit_order.setOnClickListener(listener);
        holder.fuwu_update_order.setOnClickListener(listener);

        holder.confirm_complete.setVisibility(View.GONE);// 服务完成
        holder.detele_fuwu.setVisibility(View.GONE);// 删除订单
        holder.fuwu_check_eva.setVisibility(View.GONE);// 查看评价
        holder.fuwu_to_eva.setVisibility(View.GONE);// 评价需求商
        holder.fuwu_submit_order.setVisibility(View.GONE);// 确认订单
        holder.fuwu_update_order.setVisibility(View.GONE);// 修改订单
        holder.fuwu_buttonLL.setVisibility(View.VISIBLE);//整个底部条框

        /**
         * title_type==1的时候是企业服务商
         * status:订单状态1未支付2已支付(is_service:1显示服务完成2不显示)
         * 3已完成(is_evaluation_mer:1显示评价2不显示)4已取消(删除订单)
         *
         * status:订单状态1待确认(显示确认订单)
         * 2已确认(is_update_order:1显示修改金额2不显示)
         * 3已支付(is_service:1显示完成按钮2不显示)
         * 4已完成(is_evaluation_mer:服务商是否评价1否2是)5已取消(显示删除)
         */
        if (title_type.equals("1")) {
            holder.fuwu_buttonLL.setVisibility(View.VISIBLE);
            if (list.get(position).getStatus().equals("1")) {
                holder.fuwu_state.setText("未支付");
                holder.fuwu_buttonLL.setVisibility(View.GONE);
            } else if (list.get(position).getStatus().equals("2")) {
                holder.fuwu_state.setText("已支付");
                if (list.get(position).getIs_service().equals("1")) {
                    holder.confirm_complete.setVisibility(View.VISIBLE);
                } else {
                    holder.confirm_complete.setVisibility(View.GONE);
                }
            } else if (list.get(position).getStatus().equals("3")) {
                holder.fuwu_state.setText("已完成");
                holder.detele_fuwu.setVisibility(View.VISIBLE);
                // is_evaluation_mer:服务商是否评价1否2是 is_evaluation:需求商是否评价1否2是
                if (list.get(position).getIs_evaluation_mer().equals("1")) {
                    //当是企业服务商的时候，显示删除订单订单,评价需求商，查看评价
                    holder.fuwu_to_eva.setVisibility(View.VISIBLE);
                } else {
                    holder.fuwu_to_eva.setVisibility(View.GONE);

                }
                holder.detele_fuwu.setVisibility(View.VISIBLE);
                if (list.get(position).getIs_evaluation().equals("1")) {
                    holder.fuwu_check_eva.setVisibility(View.GONE);
                } else {
                    holder.fuwu_check_eva.setVisibility(View.VISIBLE);
                }
                //                if (list.get(position).getIs_evaluation().equals("1") &&
                //                        list.get(position).getIs_evaluation_mer().equals("2")) {
                //                    holder.fuwu_buttonLL.setVisibility(View.GONE);
                //                }
                holder.detele_fuwu.setVisibility(View.VISIBLE);
            } else if (list.get(position).getStatus().equals("4")) {
                holder.fuwu_state.setText("已取消");
                holder.detele_fuwu.setVisibility(View.VISIBLE);// 删除订单
            }
            //TODO：服务商的机床改造
        } else if (title_type.equals("2")) {
            holder.fuwu_buttonLL.setVisibility(View.VISIBLE);
            if (list.get(position).getStatus().equals("1")) {
                holder.fuwu_state.setText("待确认");
                holder.fuwu_submit_order.setVisibility(View.VISIBLE);
            } else if (list.get(position).getStatus().equals("2")) {
                holder.fuwu_state.setText("已确认");

                if (list.get(position).getIs_update_order().equals("1")) {
                    holder.fuwu_buttonLL.setVisibility(View.GONE);
                } else {
                    holder.fuwu_update_order.setVisibility(View.VISIBLE);

                }
            } else if (list.get(position).getStatus().equals("3")) {
                holder.fuwu_state.setText("已支付");
                if (list.get(position).getIs_service().equals("1")) {
                    holder.confirm_complete.setVisibility(View.VISIBLE);
                } else {
                    holder.fuwu_buttonLL.setVisibility(View.GONE);
                }
            } else if (list.get(position).getStatus().equals("4")) {
                holder.fuwu_state.setText("已完成");
                holder.detele_fuwu.setVisibility(View.VISIBLE);
                // is_evaluation_mer:服务商是否评价1否2是 is_evaluation:需求商是否评价1否2是
                if (list.get(position).getIs_evaluation_mer().equals("1")) {
                    holder.fuwu_to_eva.setVisibility(View.VISIBLE);

                } else {
                    holder.fuwu_to_eva.setVisibility(View.GONE);
                }
                if (list.get(position).getIs_evaluation().equals("1")) {
                    holder.fuwu_check_eva.setVisibility(View.GONE);
                } else {
                    holder.fuwu_check_eva.setVisibility(View.VISIBLE);
                }
                if (list.get(position).getIs_evaluation().equals("1") &&
                        list.get(position).getIs_evaluation_mer().equals("2")) {
                    holder.fuwu_buttonLL.setVisibility(View.GONE);
                }
            } else if (list.get(position).getStatus().equals("5")) {
                holder.fuwu_state.setText("已取消");
                holder.detele_fuwu.setVisibility(View.VISIBLE);// 删除订单
            }
        }

        holder.order_no.setText("预约编号：" + list.get(position).getOrder_no());
        ImageLoader.getInstance().displayImage(list.get(position).getTitile_img(), holder.order_img);
        holder.order_title.setText(list.get(position).getTitle());
        holder.yuyue_content.setText(list.get(position).getType());
        holder.yuyue_money.setText("¥ " + list.get(position).getMoney());

        //        ServiceFuwuAdapter2 adapter2 = new ServiceFuwuAdapter2(context);
        //        holder.scrollview_listview.setAdapter(adapter2);
        //        holder.scrollview_listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        //            @Override
        //            public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
        //                ServiceFuwuAdapter.this.backCall.deal(R.id.scrollview_listview, position);
        //            }
        //        });
        return convertView;
    }

    class ViewHolder {
        @ViewInject(R.id.fuwu_itemll)
        LinearLayout fuwu_itemll;
        @ViewInject(R.id.item_ll)
        LinearLayout item_ll;
        @ViewInject(R.id.fuwu_buttonLL)
        LinearLayout fuwu_buttonLL;
        // @ViewInject(R.id.scrollview_listview)
        // MyListView scrollview_listview;
        @ViewInject(R.id.confirm_complete)
        TextView     confirm_complete;
        @ViewInject(R.id.detele_fuwu)
        TextView     detele_fuwu;
        @ViewInject(R.id.fuwu_check_eva)
        TextView     fuwu_check_eva;
        @ViewInject(R.id.fuwu_to_eva)
        TextView     fuwu_to_eva;
        @ViewInject(R.id.fuwu_state)
        TextView     fuwu_state;
        @ViewInject(R.id.fuwu_submit_order)
        TextView     fuwu_submit_order;
        @ViewInject(R.id.fuwu_update_order)
        TextView     fuwu_update_order;

        @ViewInject(R.id.order_img)
        ImageView order_img;
        @ViewInject(R.id.order_no)
        TextView  order_no;
        @ViewInject(R.id.order_title)
        TextView  order_title;
        @ViewInject(R.id.yuyue_content)
        TextView  yuyue_content;
        @ViewInject(R.id.yuyue_money)
        TextView  yuyue_money;

    }
}
